package com.example.jdbcdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.jdbcdemo.mapper.CollectMoneyOrderMapper;
import com.example.jdbcdemo.mapper.UserMapper;
import com.example.jdbcdemo.pojo.CollectMoneyOrder;
import com.example.jdbcdemo.service.CollectMoneyOrderServiceInterface;
import com.example.jdbcdemo.service.CollectMoneyServiceInterface;
import com.example.jdbcdemo.utils.CollectMoneyOrderFromFront;
import com.example.jdbcdemo.utils.JsonUtil;
import com.example.jdbcdemo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sell")
public class CollectMoneyOrderController {

    @Autowired
    CollectMoneyOrderServiceInterface collectMoneyOrderService;

    @Autowired
    CollectMoneyServiceInterface collectMoneyServiceInterface;
    @Autowired
    UserMapper userMapper;

    @PostMapping("/queryCollectList")
    public JsonUtil queryCollectList(){
        List list=collectMoneyServiceInterface.queryCollectOrderList();
        if (list.isEmpty())
        {
            return new JsonUtil(400,"无付款单记录","[]");
        }
        else
        {
            return new JsonUtil(0,"查询成功", JSON.toJSONString(list));
        }
    }

    @PostMapping("/add")
    public Result addCollectMoneyOrder(HttpServletRequest request, @RequestBody List<CollectMoneyOrderFromFront> collectMoneyOrderFromFront)
    {
        //操作员id从token获取
        //token中包含了用户名，根据用户名获取用户id
        String token= (String) request.getHeader("token");
        String spiltss[]=token.split("#");
        String userName=spiltss[0];
        int userId=userMapper.queryUserByname(userName).getId();
        //新增权限判断，只有老板和销售员（对应usertype为0和2）能够开销售单
        int userType=userMapper.queryUserByname(userName).getUserType();
        if (userType!=0&&userType!=2)
        {
            return Result.error(1000,"权限不足");
        }

        return collectMoneyOrderService.addCollectMoneyOrder(userId,collectMoneyOrderFromFront);
    }

    @PostMapping("/review")
    public Result review(HttpServletRequest request, @RequestBody Map<String,Integer> map)
    {
        //操作员id从token获取
        //token中包含了用户名，根据用户名获取用户id
        String token= (String) request.getHeader("token");
        String spiltss[]=token.split("#");
        String userName=spiltss[0];
        //新增权限判断，只有老板和销售员（对应usertype为0和2）能够审核销售单
        int userType=userMapper.queryUserByname(userName).getUserType();
        if (userType!=0&&userType!=2)
        {
            return Result.error(1000,"权限不足");
        }

        if(map.get("pass")==1)//0标识不通过，1标识通过
        {
            return collectMoneyOrderService.reviewPass(map.get("groupIdMapId"));
        }
        else
        {
            return collectMoneyOrderService.reviewFail(map.get("groupIdMapId"));
        }
    }

    @PostMapping("/pay")
    public Result pay(HttpServletRequest request, @RequestBody Map<String,Object> map)
    {
        //操作员id从token获取
        //token中包含了用户名，根据用户名获取用户id
        String token= (String) request.getHeader("token");
        String spiltss[]=token.split("#");
        String userName=spiltss[0];
        //新增权限判断，只有老板和收银员（对应usertype为0和3）能够收款
        int userType=userMapper.queryUserByname(userName).getUserType();
        if (userType!=0&&userType!=3)
        {
            return Result.error(1000,"权限不足");
        }

        return collectMoneyOrderService.pay((int)map.get("groupIdMapId"),((Number)map.get("payedMoney")).doubleValue());
        //refer to https://stackoverflow.com/a/27805040
    }

}
